#include "dialog_register.h"
#include "ui_dialog_register.h"
#include "inifilecontrol.h"

#include <QMessageBox>
Dialog_Register::Dialog_Register(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_Register)
{
    ui->setupUi(this);
}

Dialog_Register::~Dialog_Register()
{
    delete ui;
}

void Dialog_Register::on_pushButton_clicked()
{
    QString name = ui->lineEdit_name->text();
        QString pwd  = ui->lineEdit_pwd->text();
        iniFileControl ini;
        QString nameSrc = ini.read_ini_file(0);
        QString pwdSrc  = ini.read_ini_file(1);
        QRegExp rx;
        QMessageBox msgBox;
        if(0 == name.size())
           {
               ui->lineEdit_name->setText("昵称不能为空!");
               return ;
           }
        if(name==nameSrc)
        {
            ui->lineEdit_name->setText("用户已存在!");
            return ;
        }
           if(name.size() > 21)
           {
               ui->lineEdit_name->setText("昵称长度超过21位!");
               return ;
           }
           rx.setPatternSyntax(QRegExp::RegExp);
               //对大小写字母敏感，即区分大小写
               rx.setCaseSensitivity(Qt::CaseSensitive);
               //匹配格式为所有大小写字母和数字组成的字符串，位数不限
               rx.setPattern(QString("^[A-Za-z0-9]+$"));
               if(pwd.isEmpty())  //检测密码输入框是不是为空
               {
                   ui->lineEdit_pwd->setText("密码不能为空!");
                   return ;
               }
               else if(!rx.exactMatch(pwd))
               {
                   ui->lineEdit_pwd->setText("密码只能是数字或字母！");
                   return ;
               }
               else if(pwd.size()<6 || pwd.size()>18)
               {
                   ui->lineEdit_pwd->setText("密码长度范围必须是[6,18]！");
                   return;
               }
               if(pwd.isEmpty())  //检测密码输入框是不是为空
               {
                   ui->lineEdit_pwd->setText("密码不能为空!");
                   return;
               }

        if(nameSrc=="")
            nameSrc +=name;
        else
        {
            nameSrc += ",";
            nameSrc +=name;
        }
        if(pwdSrc=="")
            pwdSrc +=pwd;
        else
        {
            pwdSrc += ",";
            pwdSrc +=pwd;
        }
        ini.write_ini_file(nameSrc,pwdSrc);
        accept();
}

void Dialog_Register::on_checkBox_clicked()
{
    if(ui->checkBox->isChecked())
        {
            ui->lineEdit_pwd->setEchoMode(QLineEdit::Normal);   //显示密码
        }
        else
        {
           ui->lineEdit_pwd->setEchoMode(QLineEdit::Password);   //隐藏密码
        }
}
